<?php

class Project {
    
    private $pid;
    public  $name;
    public  $description;
    private $dateCreated;
    public  $dateStart;
    public  $dateEnd;
    public  $priority;
            
    public function __construct($pid=0) {
        if($pid < 1) {
            return;
        }
        if(!count($data = ProjectDAO::getProjectData($pid))) {
            return;
        }
        $this->setData($data);
        $this->pid = $data['pid'];
        $this->dateCreated = $data['dateCreated'];
    }

    public function getPid() {
        return $this->pid;
    }
    
    public function getDateCreated() {
        return $this->dateCreated;
    }
    
    public function create() {
        if(!$this->name || !$this->description) {
            $err = Error::instance();
            $err->add("Nazwa i opis projektu muszą być ustawione.");
            return false;
        }
        
        if(ProjectDAO::exists($this)) {
            $err = Error::instance();
            $err->add("Projekt o takiej nazwie już istnieje.");
            return false;
        }
        
        $this->dateCreated = time();
        $this->pid = ProjectDAO::create($this);
        return $this->pid;
    }
    
    public function delete() {
        if($this->pid < 1) {
            return false;
        }
        
        if(!ProjectDAO::exists($this)) {
            $err = Error::instance();
            $err->add("Podany projekt nie istnieje.");
            return false;
        }
        
        $tasks = $this->getTasks();
        $currentlyAtProgress = array();
        foreach($tasks as $key => $task) {
            if(ProjectDAO::taskAtProgress($task)) {
                $currentlyAtProgress[] = $task;
            }
        }
        if(count($currentlyAtProgress)) {
            $err = Error::instance();
            $atWork = '<ul>';
            foreach($currentlyAtProgress as $key => $task) {
                $arWork .= '<li>' . $task->name . '</li>';
            }
            $atWork = '</ul>';
            $err->add("Następujące zadania projektu są w trakcie wykonywania:" . $atWork);
            return false;
        }
        ProjectDAO::removeWorks($this);
        ProjectDAO::removeSignedUsers($this);
        ProjectDAO::removeTasks($this);
        return ProjectDAO::removeProject($this);
    }
    
    public function update() {
        if($this->pid < 1) {
            return false;
        }
        if(!$this->name || !$this->description) {
            $err = Error::instance();
            $err->add("Nazwa i opis projektu muszą być ustawione.");
            return false;
        }
        return ProjectDAO::update($this);
    }
    
    public function getTasks() {
        if($this->pid < 1) {
            return false;
        }
        
        return ProjectDAO::getTasks($this);
    }
    
    public function getUsers() {
        if($this->pid < 1) {
            return array();
        }
        
        $tasks = $this->getTasks();
        $users = array();
        foreach($tasks as $key => $task) {
            $users[] = array(new AnyUser((int)$task->getUser()),$task);
        }
        
        return $users;
    }
    
    public function setData($data) {
        foreach($data as $key => $val) {   
            if(property_exists(get_class($this),$key) && $key != 'pid' && $key != 'dateCreated') {
                $this->$key = $val;
            }
        }
    }
}

?>
